<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/11/25
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>

<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="rolename" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入角色名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">角色描述:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="roledesc" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入角色描述" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否可用:</label>
            <div class="layui-input-inline">
                <input type="radio" name="available" id="available_on" value="1" checked="checked" title="可用">
                <input type="radio" name="available" id="available_off" value="0" title="不可用">
            </div>
        </div>
        <div class="layui-inline">
            <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit  lay-filter="doSearch">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>
</form>

<table id="roleTable" lay-filter="roleTable"></table>

<script id="toolbarRole" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="roleAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="delBatch">批量删除</button>
    </div>
</script>

<script id="barRole" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="selRolerole">分配角色</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
</script>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">

        <div class="layui-form-item">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="roleid">
                <input type="text" name="rolename" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色描述:</label>
            <div class="layui-input-block">
                <input type="text" name="roledesc" placeholder="请输入角色描述" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否可用:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" checked title="可用">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<div style="display: none;padding: 20px" id="roleDiv">
    <ul id="demoTree" class="dtree" data-id="0"></ul>
</div>

<script src="${pageContext.request.contextPath}/static/layuicms2.0/layui/layui.js"></script>
    <script>
        var roleTree;
        var roleTable;
        var miniIndex;
        var url;
        var demoTree;
        layui.extend({
            dtree: '${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
        }).use(['table','form','layer','jquery','dtree'], function(){
            var table = layui.table
            var form = layui.form
            var layer = layui.layer
            var $ = layui.jquery
            var dtree = layui.dtree

            // 初始table
            roleTable = table.render({
                elem:"#roleTable"
                ,url:"/car/role/init"
                ,page: {limit:5 ,limits:[5,10,15]} //开启分页
                ,cellMinWidth:80
                ,toolbar: '#toolbarRole' //开启头部工具栏，并为其绑定左侧模板
                ,cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'roleid', title: '角色ID',  fixed: 'center'}
                    ,{field: 'rolename', title: '角色名称',   fixed: 'center'}
                    ,{field: 'roledesc', title: '描述',  fixed: 'center'}
                    ,{field: 'available', title: '是否可用',   fixed: 'center',templet:function(data) {
                        return data.available == 1 ? "可用" :"<font color=red >不可用</font>"
                    }}
                    ,{fixed: 'right', title:'操作', toolbar: '#barRole', width:280}
                ]]
            })

            // form表单提交  -- 查询功能
            form.on("submit(doSearch)",function(data) {
                roleTable.reload({
                    url:"/car/role/init"
                    ,where :data.field
                    ,page:{
                        curr:1
                    }
                })
                return false;
            })

            // 头部工具栏添加事件
            table.on('toolbar(roleTable)',function(obj) {
                if(obj.event == "roleAdd"){ // 点击新增按钮执行的内容
                    miniIndex = layer.open({
                        type: 1
                        , title: "添加菜单"
                        , content: $("#saveOrUpdateDiv")
                        , area: ['750px', '450px']
                        , success: function (layero, index) {
                            // 页面数据要清除
                            $("#dataFrm")[0].reset()
                            url="/car/role/add"
                        }
                    })
                }else if(obj.event == "delBatch"){
                var checkStatus = table.checkStatus(obj.config.id)//获取选中的条数
                    if(checkStatus.data.length==0){//选择的条数=0就提示
                        layer.msg("您至少选择一条进行删除")
                    }
                    var ids=new Array() //申明数组
                    for(var i=0;i<checkStatus.data.length;i++){
                          ids.push(checkStatus.data[i].roleid)//把选中条数的Id循环装进ids数组
                    }
                    $.post("/car/role/deleteAll",{"ids":ids},function (r) {//传ids到contorlle层进行批量删除
                        layer.msg(r.msg)
                        roleTable.reload()
                    })
                }
            })




            form.on('submit(doSubmit)', function (data) {
                $.post(url, data.field, function (r) {
                    if (r.code == 0) {
                        layer.close(miniIndex)
                        roleTable.reload({
                            page: {
                                curr: 1
                            }
                        })
                        layer.msg(r.msg, {icon: 1})
                    } else {
                        layer.msg(r.msg, {icon: 2})
                    }
                })
                return false;
            })




            //监听行工具事件
            table.on('tool(roleTable)', function(obj){
                var data = obj.data
                if(obj.event == "edit"){ // 编辑
                    roleUpdate(data)


                }else  if(obj.event == "del"){ // 删除
                    del(data)
                } else if(obj.event == "selRolerole"){
                    layer.open({
                        type:1 // 1（页面层）
                        , title:"分配【"+data.rolename+"】角色"
                        , content:$("#roleDiv")
                        , area : ['350px','500px']
                        , success:function(layero, index) {
                            // 初始化树
                            demoTree = dtree.render({
                                elem: "#demoTree",
                                url: "/car/role/roleMenuInit?roleid="+data.roleid // 使用url加载（可与data加载同时存在）
                                ,dataStyle: "layuiStyle",  //使用layui风格的数据格式
                                response:{message:"msg",statusCode:0}  //修改response中返回数据的定义
                                ,icon: "2"  //修改二级图标样式
                                ,checkbar: true  //开启复选框
                            });
                        }
                        ,btn :['确认分配','取消分配']
                        ,yes: function(index, layero){

                            // 点击按钮获取所有选中节点值
                            var params = dtree.getCheckbarNodesParam("demoTree");
                            // layer.msg(JSON.stringify(params));

                            // 当前角色id data.roleid

                            var ids = new Array()
                            for(var i = 0 ; i< params.length ;i++){
                                ids.push(params[i].nodeId)
                            }


                            $.post("/car/role/selRolerole" , {"roleid":data.roleid , "ids":ids} ,function (r) {
                                if(r.code== 0){
                                    layer.close(index)
                                }
                                layer.msg(r.msg)
                            })

                        }


                    })
                }
            })


            function roleUpdate(data) {
                miniIndex = layer.open({
                    type: 1
                    , title: "修改菜单"
                    , content: $("#saveOrUpdateDiv")
                    , area: ['750px', '450px']
                    , success: function (layero, index) {
                        form.val("dataFrm", data)
                        url="/car/role/update"
                    }

                })
            }

            function del(data){
                layer.confirm("您确定要删除该角色吗",function (ind) {
                    $.post("/car/role/delete",{"roleid":data.roleid},function (r) {
                                layer.msg(r.msg)
                                roleTable.reload()
                    })
                    layer.close(ind)
                })
            }


        });


    </script>
</body>
</html>
